/* Figure in Appendix C: Racial Segregation Correlations */

local PATH_FIG = "~/Downloads/figures"

quietly{  

/* Local Partisan Offices */
  use wide_local_partisan_races, clear
  gen A_pct = 100 * D_choice/(D_choice + R_choice)
  gen competitive = (A_pct > 5 & A_pct < 95)
  keep if competitive == 1
  
  gen A_pct_D_pid = 100 * D_choice_D_pid/(D_choice_D_pid + R_choice_D_pid)
  gen A_pct_R_pid = 100 * D_choice_R_pid/(D_choice_R_pid + R_choice_R_pid)
  gen apg_DR        = abs(A_pct_D_pid - A_pct_R_pid)
  
   //* merge racial seg info
  gen county = proper(place) //create county var 
  gen county_name = proper(substr(place, 1, strpos(county, " County") - 1))  

  replace county_name = "McLean" if county_name == "Mclean" & state == "IL"
  replace county_name = "DeKalb" if county_name == "Dekalb" & state == "GA"
  replace county_name = "McHenry" if county_name == "Mchenry" & state == "IL"
  replace county_name = "Chattooga" if county_name == "Chatooga" & state == "GA"
  replace county_name = "McDuffie" if county_name == "Mcduffie" & state == "GA"
  replace county_name = "McKinley" if county_name == "Mckinley" & state == "NM"
  replace county_name = "Emanuel" if county_name == "Emmanuel" & state == "GA"

  drop county
  gen county = county_name

  keep if dist == "." | dist == "CHAIR" | dist == "CENTRAL,1 " 
  
  //* merge size of voters
  merge m:1 state county using "APSR_counties_used.dta"
  gen coverage = T/presvotes_county
  keep if coverage > 0.6
  drop _merge
   
  //* merge racial seg info
  merge m:1 state county using "fgC5_county_race_seg.dta"
  rename d_wa_ba seg_black // more intuitive name

  //* R-squares and graphs 
  reg apg_DR seg_black
  local r2 = e(r2)
  noisily display "R2 for local partisan offices " %4.2f `r2'
   
  lowess apg_DR seg_black, plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
    title("Local Partisan Offices", size(huge)) msize(.4) mcolor(gs8) ///
    xtitle("Racial Segregration Index", size(vlarge)) ytitle("Absolute Partisan Gap", size(vlarge)) ///
	xlabel(0.25 .5 .75, labsize(vlarge)) ylabel(0 20 40 60 80 100, labsize(vlarge)) 
  graph export "`PATH_FIG'/local_partisan_offices_vs_seg_black.pdf", replace
}

 
/* Local Nonpartisan Offices */
quietly {
  use wide_local_nonpartisan_races, clear
  gen A_pct = 100 * A_choice/(A_choice + B_choice)
  gen competitive = (A_pct > 5 & A_pct < 95)
  keep if competitive == 1
  
  gen A_pct_D_pid = 100 * A_choice_D_pid/(A_choice_D_pid + B_choice_D_pid)
  gen A_pct_R_pid = 100 * A_choice_R_pid/(A_choice_R_pid + B_choice_R_pid)
  gen apg_DR        = abs(A_pct_D_pid - A_pct_R_pid)
 
  gen county = proper(place)
  gen county_name = proper(substr(place, 1, strpos(county, " County") - 1))  
  replace county_name = "El Dorado" if county_name == "El Dorado Hills" & state == "CA"
   
  drop county
  rename county_name county
   
  //* merge size of voters
  merge m:1 state county using "APSR_counties_used.dta"
  gen coverage = T/presvotes_county
  keep if coverage > 0.6
  drop _merge
 
  //* merge racial seg info
  merge m:1 state county using "fgC5_county_race_seg.dta"
  rename d_wa_ba seg_black // more intuitive name

  //* R-squares and graphs 
  reg apg_DR seg_black
  local r2 = e(r2)
  noisily display "R2 for local non-partisan offices " %4.2f `r2'
  
  lowess apg_DR seg_black, plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
  title("Local Nonpartisan Offices", size(huge)) msize(.4) mcolor(gs8) ///
    xtitle("Racial Segregration Index", size(vlarge)) ytitle("Absolute Partisan Gap", size(vlarge))  ///
	xlabel(0 .25 .5 .75, labsize(vlarge)) ylabel(0 20 40 60 80 100, labsize(vlarge)) 
  graph export "`PATH_FIG'/local_nonpartisan_offices_vs_seg_black.pdf", replace
}

 
  
/* Local Ballot Measures  */
quietly {
  use wide_local_ballot_measures, clear
  
  gen OTH_choice = N - U_choice - Y_choice - N_choice
  gen pct_YN = (Y_choice + N_choice)/(Y_choice + N_choice + OTH_choice)
  drop if pct_YN < .75
  drop OTH_choice

  gen Y_pct = 100 * Y_choice/(Y_choice + N_choice)
  gen competitive = (Y_pct > 5 & Y_pct < 95)
  keep if competitive == 1
  
  gen Y_pct_D_pid = 100 * Y_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid)
  gen Y_pct_R_pid = 100 * Y_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid)
  gen apg_DR        = abs(Y_pct_D_pid - Y_pct_R_pid)  
 
  gen county = proper(place)
  gen county_name = proper(substr(place, 1, strpos(county, " County") - 1))  
  
  replace county_name = "Hidalgo" if county_name == "Hildago" & state == "TX"
  replace county_name = "El Dorado" if county_name == "El Dorado Hills" & state == "CA"
   
  drop county
  gen county = county_name
   
  //* merge size of voters
  merge m:1 state county using "APSR_counties_used.dta"
  gen coverage = T/presvotes_county
  keep if coverage > 0.6
  drop _merge
 
  //* merge racial seg info
  merge m:1 state county using "fgC5_county_race_seg.dta"     
  rename d_wa_ba seg_black // more intuitive name  
 
  // output
  reg apg_DR seg_black
  local r2 = e(r2)
  noisily display "R2 for local ballot measures " %4.2f `r2'
  

  lowess apg_DR seg_black, plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
    title("Local Ballot Measures", size(huge)) msize(.4) mcolor(gs8) ///
    xtitle("Racial Segregration Index", size(vlarge)) ytitle("Absolute Partisan Gap", size(vlarge)) ///
	xlabel(0 .25 .5 .75, labsize(vlarge)) ylabel(0 20 40 60 80 100, labsize(vlarge)) 
  graph export "`PATH_FIG'/local_ballot_measures_vs_seg_black.pdf", replace  

}
